﻿using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
using XNodeEditor;

namespace BearUtil.Hidden
{
    /// <summary>
    /// 自定义拓展节点类型
    /// </summary>
    public class BearDialog : BearNode
    {
        [Sirenix.OdinInspector.ReadOnly]
        public int id;
        /// <summary>
        /// 可能不是必须的一些属性
        /// </summary>
        [LabelText("基本属性")] public BasicProperty basic;
        
        /// <summary>
        /// 定义对话内容界面
        /// </summary>
        [HideInInspector, BoxGroup("快速编辑"), LabelText("编辑界面"), OnValueChanged("BindGraph")]
        public DialogGraph selfGraph;

        public BearDialog()
        {
            if (basic == null)
                basic = new BasicProperty();

            if (basic.audioData.SyncFontSpeedCallback == null)
                basic.audioData.SyncFontSpeedCallback = SyncFontSpeed;
        }

        public void SyncFontSpeed()
        {
            for (var i = 0; i < selfGraph.nodes.Count; i++)
            {
                var node = selfGraph.nodes[i] as TalkNode;
                if (node == null)
                    continue;

                node.FontSpeed = basic.audioData.FontSpeed;
            }
        }

        public void BindGraph(DialogGraph dialogGraph)
        {
            if (dialogGraph == null)
            {
                if (EditorUtility.DisplayDialog("错误", "Graph 节点丢失，找人来修", "确认"))
                    return;
            }

            this.selfGraph = dialogGraph;
            selfGraph.owner = this;
        }

        [Button("编辑")]
        public void OpenGraph()
        {
            NodeEditorWindow.Open(selfGraph);
        }

        public void OnDestroy()
        {
            selfGraph.DeleteSelf();
        }
    }   
}
